/*
    输入一个链表，输出该链表中倒数第k个结点。
*/

public class Problem10 {
    public static ListNode FindKthToTail(ListNode head,int k) {
        ListNode tmp = head;
        if(head == null){
            return null;
        }
        int length = 1;
        int start = 0;
        while (tmp.next!=null){
            tmp = tmp.next;
            length++;
        }
        if(k>length){
            return null;
        }
        while (start<length-k){
            head = head.next;
            start++;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode head =new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        System.out.println(FindKthToTail(head,5).val);
    }
}
